<template>
    <div class="plan">
        <el-table
            id="cfsgateout-table-cfsgateout"
            ref="tableRef"
            :data="tableData"
            v-bind="tableDefaultAttribute"
            :max-height="tableHeight"
            row-key="id"
            table-layout="auto"
            highlight-current-row
            @current-change="handleCurrentChange"
        >
            <template #empty>
                <span>无数据</span>
            </template>

            <template v-for="col in tableColumns" :key="col.prop">
                <el-table-column
                    v-if="!col.isHidden"
                    :align="col.align"
                    :prop="col.prop"
                    :label="col.label"
                    :formatter="col.formatter"
                    :type="col.type"
                    :fixed="col.fixed"
                    :width="col.width"
                >
                </el-table-column>
            </template>
        </el-table>
    </div>
    <div class="cntr">
        <el-form
            ref="formRef"
            :model="formData"
            :rules="rules"
            label-width="100px"
            :size="'default'"
            status-icon
        >
            <el-row :gutter="2">
                <el-col :span="6">
                    <el-form-item label="箱号" prop="cntrs">
                        <el-select
                            v-model.trim="cntrs"
                            :clearable="true"
                            filterable
                            multiple
                            placeholder="选择箱号"
                        >
                            <el-option
                                v-for="optValue in allowedCntrs"
                                :key="optValue.value"
                                :label="optValue.value"
                                :value="optValue.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="出场日期" prop="leav_port_tim">
                        <el-date-picker
                            v-model="formData.leav_port_tim"
                            type="datetime"
                            :size="'default'"
                        />
                    </el-form-item>
                </el-col>

                <el-col :span="4">
                    <el-form-item label="尺寸" prop="cntr_siz_cod">
                        <el-select
                            v-model="formData.cntr_siz_cod"
                            :clearable="true"
                            disabled
                        >
                            <el-option
                                v-for="optValue in cntrSiz"
                                :key="optValue.value"
                                :label="optValue.label"
                                :value="optValue.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item label="箱型" prop="cntr_typ_cod">
                        <el-select
                            v-model="formData.cntr_typ_cod"
                            :clearable="true"
                            disabled
                        >
                            <el-option
                                v-for="optValue in cntrTyp"
                                :key="optValue.value"
                                :label="optValue.value"
                                :value="optValue.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="2">
                <el-col :span="6">
                    <el-form-item label="放箱单号" prop="release_no">
                        <el-input v-model.trim="formData.release_no" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item label="提箱车号" prop="out_truck_no_c">
                        <el-input v-model.trim="formData.out_truck_no_c" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="司机电话" prop="out_truck_tel">
                        <el-input v-model.trim="formData.out_truck_tel" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
    <div class="btn-group">
        <el-button-group>
            <el-button
                class="btn"
                type="primary"
                v-debounce="onSave"
                size="large"
                >出场</el-button
            >
            <el-button
                class="btn"
                type="primary"
                @click="onRefresh"
                size="large"
                >刷新</el-button
            >
        </el-button-group>
    </div>
</template>
<script setup lang="ts" name="CfsGateOut">
import { ref, reactive, onMounted } from 'vue';
import { storeToRefs } from 'pinia';
import type { FormInstance, FormRules, TableRefs } from 'element-plus';
import type { CntrModel, OutPlanModel } from '@/common/types/container';
import { Request } from '@/app/request';
import { tableDefaultAttribute, tableColumnDefault } from '@/hooks/useTable';
import { formatDate, transFormatter, validateCntr } from '@/common/tools';
import { useAppStore } from '@/store/app';
import { TableColumn, SelectOption } from '@/common/types/app';
import { isEmpty } from 'lodash';
import textupperinput from '@/components/forms/TextUpperInput.vue';
import { ValidateFieldsError } from 'async-validator';

const tableRef = ref<TableRefs>();
const tableData = ref<OutPlanModel[]>([]);
const formRef = ref<FormInstance>();
const formData = reactive<CntrModel>({});
const cntrs = ref<string[]>([]);
const tableHeight = ref(100);
// const currentRow = ref<OutPlanModel>(); // 用于存储当前选中的行
const allowedCntrs = ref<SelectOption[]>([]);

const { cntrSiz, cntrTyp, SelectOptionCarry, cntrGateOutStat } =
    storeToRefs(useAppStore());
const onFormatter = (_row: any, column: any, cellValue: any) => {
    let data: { value: string | number; label: string }[];
    if (column['property'] === 'carry_id') {
        data = SelectOptionCarry.value;
    } else if (column['property'] === 'stat_cod') {
        data = cntrGateOutStat.value;
    } else {
        return cellValue;
    }

    return transFormatter(cellValue, data);
};
const tableColumns = reactive<TableColumn[]>([
    {
        ...tableColumnDefault,
        order: 1,
        prop: 'id',
        label: '计划号',
        fixed: 'left',
        formFieldItemType: 'input',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 1,
        prop: 'plan_name',
        label: '计划名称',
        sortable: 'custom',
        fixed: 'left',
        formFieldItemType: 'input',
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 8,
        prop: 'stat_cod',
        label: '作业目的',
        sortable: 'custom',
        formatter: onFormatter,
        formFieldItemType: 'single',
        optionalValues: cntrGateOutStat.value,
        width: 150,
    },
    {
        ...tableColumnDefault,
        order: 2,
        prop: 'plan_date',
        label: '放箱日期',
        sortable: 'custom',
        formatter: formatDate,
        formFieldItemType: 'date',
        width: 200,
    },
    {
        ...tableColumnDefault,
        order: 5,
        prop: 'cntr_siz_cod',
        label: '箱尺寸',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 6,
        prop: 'cntr_typ_cod',
        label: '箱型',
        sortable: 'custom',
        width: 80,
    },
    {
        ...tableColumnDefault,
        order: 7,
        prop: 'cntr_num',
        label: '箱量',
        sortable: 'custom',
        width: 80,
    },

    {
        ...tableColumnDefault,
        order: 11,
        prop: 'bill_no',
        label: '放箱单号',
        sortable: 'custom',
        width: 200,
    },

    {
        ...tableColumnDefault,
        order: 16,
        prop: 'cntr_work_num',
        label: '已作业箱量',
        sortable: 'custom',
        width: 120,
    },

    {
        ...tableColumnDefault,
        order: 18,
        prop: 'remark',
        label: '备注',
        sortable: 'custom',
        width: 400,
    },
]);
const rules = reactive<FormRules>({
/*     cntr: [
        {
            type: 'array',
            required: true,
            message: '请选择箱号',
            trigger: 'blur',
        },
    ], */
    leav_port_tim: [
        { required: true, message: '请输入出场时间', trigger: 'blur' },
    ],
});
const onRefresh = async () => {
    formRef.value?.resetFields();
    cntrs.value.length = 0;
    // currentRow.value = undefined;

    const req = new Request('堆场未完成出场计划查询');
    const r = await req.post();
    tableData.value.length = 0;

    if (!isEmpty(r.obj)) {
        tableData.value = r.obj;
    }
};
onMounted(() => {
    tableHeight.value = Math.floor((window.innerHeight / 3) * 2);
    onRefresh();
});
/* const copyPlanData = () => {
    console.log(currentRow.value);
    
    formData.cntr_siz_cod = currentRow.value?.cntr_siz_cod;
    formData.cntr_typ_cod = currentRow.value?.cntr_typ_cod;
    formData.release_no = currentRow.value?.release_no;
    console.log(formData.value);
    
}; */
const handleCurrentChange = async (val: OutPlanModel | undefined) => {
    formRef.value?.resetFields();
    // currentRow.value = val; // 更新当前选中的行

    // copyPlanData();
    formData.out_truck_sn = val?.id;
    formData.cntr_siz_cod = val?.cntr_siz_cod;
    formData.cntr_typ_cod = val?.cntr_typ_cod;
    formData.release_no = val?.bill_no;
    allowedCntrs.value.length = 0;
    if (val?.id) {
        const req = new Request('堆场未完成计划预定箱查询', {
            plan_id: val?.id,
        });
        const r = await req.post();
        allowedCntrs.value.length = 0;
        if (!isEmpty(r.obj)) {
            allowedCntrs.value = r.obj;
        }
    }
};
const onSave = async () => {
    const formValid = await formRef.value?.validate(
        (isValid: boolean, _invalidFields?: ValidateFieldsError) => {
            // return isValid;
            return;
        },
    );

    if (formValid) {
        /* console.log({
            cntrs: cntrs.value,
            out_truck_sn: formData.out_truck_sn,
            leav_port_tim: formData.leav_port_tim,
            out_truck_no_c: formData.out_truck_no_c,
            out_truck_tel: formData.out_truck_tel,
        }); */
        const req = new Request('闸口箱出场', {
            cntrs: cntrs.value,
            out_truck_sn: formData.out_truck_sn,
            leav_port_tim: formData.leav_port_tim,
            out_truck_no_c: formData.out_truck_no_c,
            out_truck_tel: formData.out_truck_tel,
        });
        const r = await req.post();
        if (r.code > 100) {
            onRefresh();
        }
    }
};
</script>
<style lang="scss" scoped>
.cntr {
    margin-bottom: 20px;
    padding: 10px 10px;
    border: solid 2px var(--el-color-primary);
    box-shadow: 10px 10px 5px rgba(100, 100, 100, 0.5);
}
.plan {
    margin-bottom: 20px;
    padding-top: 10px 10px;
    border: solid 2px var(--el-color-primary);
    box-shadow: 10px 10px 5px rgba(100, 100, 100, 0.5);
}
.btn-group {
    .btn {
        margin-right: 5px;
    }
}
</style>
